<template>
  <div class="big-box">
    <div class="search-container">
      <el-form
        :inline="true"
        label-width="100px"
        class="word"
        ref="queryFormRef"
        :model="queryParams"
      >
        <el-form-item label="单词" prop="word">
          <el-input
            v-model="queryParams.word"
            placeholder="单词"
            clearable
            style="width: 200px; margin-right: 20px"
            @keyup.enter="handleQuery"
        /></el-form-item>
        <el-form-item class="handleQuery">
          <el-button type="primary" @click="handleQuery"
            ><i-ep-search />搜索</el-button
          >
          <el-button @click="resetQuery"><i-ep-refresh />重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-card shadow="never" class="table-container">
      <el-table
        v-loading="loading"
        highlight-current-row
        border
        ref="WordFormRef"
        :data="WordList"
      >
        <el-table-column label="单词" prop="word" align="center" />
        <!-- show-overflow-tooltip -->
        <el-table-column label="状态" prop="flag" width="100" align="center">
          <template #default="scope">
            <el-tag v-if="scope.row.flag === 1" type="success">已背</el-tag>
            <el-tag v-else flag="info">未背</el-tag>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" align="center" width="200">
          <template #default="scope">
            <el-button
              type="primary"
              link
              size="small"
              @click="changeChannel(scope.row.word, scope.row)"
              ><i-ep-delete />修改状态</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-if="total > 0"
        v-model:total="total"
        v-model:page="queryParams.page"
        v-model:limit="queryParams.pageSize"
        @pagination="handleQuery"
      />
    </el-card>
  </div>
</template>

<script lang="ts" setup>
import { ref } from "vue";
import {
  getWordComPage,
  getWordComMenuIds,
  updateWordCom,
  getUserIds,
} from "@/api/word/index";
import {
  WordComForms,
  WordComQuery,
  WordComPageVO,
  WordComPageResult,
  UserPageVO,
} from "@/api/word/types";

const value = ref();
// const channelList = ref([]);
const editDialog = ref();
const removeIds = ref([]); // 删除用户ID集合 用于批量删除
// const removeId = ref([]); // 删除用户ID集合 用于批量删除
const loading = ref(false);
const WordList = ref<WordComPageVO[]>();
const userList = ref<UserPageVO[]>();
const total = ref(0);
const queryFormRef = ref(ElForm);
const queryParams = reactive<WordComQuery>({
  word: "",
  page: 1,
  pageSize: 10,
});
const UserPageVOs = reactive<UserPageVO>({});

/** 查询 */
function handleQuery() {
  loading.value = true;
  getWordComPage(queryParams)
    .then(({ data }) => {
      WordList.value = data.items;
      total.value = data.total;
    })
    .finally(() => {
      loading.value = false;
    });
}
/** 重置查询 */
function resetQuery() {
  queryFormRef.value.resetFields();
  queryParams.page = 1;
  handleQuery();
}

onMounted(() => {
  handleQuery();
});

const WordFormRef = ref(ElForm);

const formData = reactive<WordComForms>({
  id: "",
  word: "",
  flag: "",
});
/** 关闭新增修改表单弹窗 */
function closeDialogs() {
  dialog.visible = false;
  resetForm();
}

/** 重置表单 */
function resetForm() {
  WordFormRef.value.resetFields();
  WordFormRef.value.clearValidate();

  formData.id = "";
  formData.word = "";
  formData.flag = "";
}

function changeChannel(word?: string, row?: any) {
  // const WordId = [id || removeIds.value].join(",");
  ElMessageBox.confirm("确认更改?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(() => {
    const words = word;
    loading.value = true;
    formData.word = words;
    console.log(word);
    if (row.flag === 1) {
      row.flag = 0;
    } else {
      row.flag = 1;
    }
    formData.flag = row.flag;
    // formData.word = row.word;
    updateWordCom(words, formData)
      .then(({ data }) => {
        ElMessage.success("操作成功");
        resetQuery();
      })
      .finally(() => (loading.value = false));
  });
}
</script>

<style scoped lang="scss">
.form-container {
  display: flex;
  flex-wrap: wrap;
}

.handleQuery {
  margin-left: auto;
}
</style>
